
#include "stdafx.h"

#include <MyViews/RubberRect.h>

RubberRect::RubberRect(PanView * pWnd)
	: m_pWnd(pWnd)
{
	m_Visible = false;
	m_Shown = false;
}

void RubberRect::PinDown(CPoint p)
{
	m_Box = CRect(p.x, p.y, p.x, p.y);
	if (m_Visible) {
		Hide();
		Show();
	}
}

void RubberRect::Stretch(CPoint p)
{
	m_Box.right = p.x;
	m_Box.top = p.y;

	if (m_Visible) {
		Hide();
		Show();
	}
}

void RubberRect::Draw(CDC * pDC)
{
	m_Rect = GetBox();
	pDC->DrawFocusRect(&m_Rect);
	m_Rect.OffsetRect(m_pWnd->GetOrigin());
}

void RubberRect::Show()
{
	if (m_Shown)
		return;
	m_pWnd->UpdateWindow();
	Draw(m_pWnd->GetDC());
	m_Shown = true;
}

void RubberRect::Hide()
{
	if (!m_Shown)
		return;
	CRect r(m_Rect);
	r.OffsetRect(- m_pWnd->GetOrigin());
	m_pWnd->GetDC()->DrawFocusRect(&r);
	m_Shown = false;
}

CRect RubberRect::GetBox() 
{
	CRect rect = m_Box;
	rect.NormalizeRect();
	return rect;
}

void RubberRect::SetVisible(bool visible)
{
	m_Visible = visible;
	if (m_Visible)
		Show();
	else
		Hide();
}
